package org.xiaog.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author xiaog
 */
public class DruidUtil {

    private static final Properties properties = new Properties();

    private static DataSource dataSource;

    static {
        try(InputStream is = DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties")) {
            properties.load(is);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static DataSource getDataSource() {
        if (dataSource == null) {
            try {
                dataSource = DruidDataSourceFactory.createDataSource(properties);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return dataSource;
    }

    public static Connection getConnection() {
        Connection con = null;
        try {
            con = getDataSource().getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

}
